import 'dart:math';

import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}
void test(inout int number){

}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: MyHomePageBody(),
    );
  }
}

class MyHomePageBody extends StatefulWidget {
  const MyHomePageBody({Key? key}) : super(key: key);

  @override
  _MyHomePageBodyState createState() => _MyHomePageBodyState();
}

class _MyHomePageBodyState extends State<MyHomePageBody> {
  final scrollerViewController = ScrollController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    scrollerViewController.addListener(() {
      print("ListViewScroller offset${scrollerViewController.offset} ");
      if (scrollerViewController.offset > 1000) {
        scrollerViewController.animateTo(0,
            duration: Duration(seconds: 3), curve: Curves.easeIn);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: NotificationListener(
        child: ListView(
          controller: scrollerViewController,
          children: List.generate(100, (index) {
            return ListTile(
              title: Text("hello world $index"),
              leading: Icon(Icons.people),
              trailing: Icon(Icons.delete),
            );
          }),
        ),
        onNotification: (notification) {
          if (notification is ScrollStartNotification) {
            print(
                "开始滚动  当前滚动距离: ${notification.metrics.pixels}, 总滚动长度${notification.metrics.maxScrollExtent}");
          } else if (notification is ScrollEndNotification) {
            print(
                "结束滚动  当前滚动距离: ${notification.metrics.pixels}, 总滚动长度${notification.metrics.maxScrollExtent}");
          } else if (notification is ScrollUpdateNotification) {
            print(
                "正在滚动  当前滚动距离: ${notification.metrics.pixels}, 总滚动长度${notification.metrics.maxScrollExtent}");
          }
          return true;
        },
      ),
    );
  }

  CustomScrollView CustomScrollViewDemo() {
    return CustomScrollView(
      slivers: [
        SliverAppBar(
          title: Text(
            "Sliver",
            style: TextStyle(fontSize: 20, color: Colors.white),
          ),
          pinned: true,
          expandedHeight: 150,
          flexibleSpace: Image.network(
            "https://pics7.baidu.com/feed/e4dde71190ef76c6283a22fb6a0ce4fcae516713.jpeg?token=d6bf8693df3668d8aa220934978970e0&s=60B7179C7E9255F79CB428E2030070A2",
            fit: BoxFit.cover,
          ),
        ),
        SliverList(
            delegate: SliverChildBuilderDelegate((context, index) {
          return ListTile(
            title: Text("SliverList$index"),
          );
        }, childCount: 100)),
        SliverGrid(
            delegate: SliverChildBuilderDelegate((context, index) {
              return Container(
                width: 100,
                height: 100,
                decoration: BoxDecoration(
                    color: Color.fromARGB(255, Random().nextInt(255),
                        Random().nextInt(255), Random().nextInt(255)),
                    borderRadius: BorderRadius.circular(3)),
              );
            }),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                crossAxisSpacing: 10,
                mainAxisSpacing: 10,
                childAspectRatio: 1.2)),
      ],
    );
  }

  GridView GridViewDemo2() {
    return GridView.builder(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            crossAxisSpacing: 10,
            mainAxisSpacing: 10,
            childAspectRatio: 1.2),
        itemBuilder: (context, index) {
          return Container(
            width: 100,
            height: 100,
            decoration: BoxDecoration(
                color: Color.fromARGB(255, Random().nextInt(255),
                    Random().nextInt(255), Random().nextInt(255)),
                borderRadius: BorderRadius.circular(3)),
          );
        });
  }

  Padding GridViewDemo1() {
    return Padding(
      padding: EdgeInsets.all(10),
      child: GridView(
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3,
            crossAxisSpacing: 10,
            mainAxisSpacing: 10,
            childAspectRatio: 1.2),
        children: List.generate(100, (index) {
          return Container(
            width: 100,
            height: 100,
            decoration: BoxDecoration(
                color: Color.fromARGB(255, Random().nextInt(255),
                    Random().nextInt(255), Random().nextInt(255)),
                borderRadius: BorderRadius.circular(3)),
          );
        }),
      ),
    );
  }
}
